package com.briup.es.web;

import com.briup.es.auth.SecurityUserContext;
import com.briup.es.config.response.Result;
import com.briup.es.domain.vo.AddressVO;
import com.briup.es.service.AddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {

    private final AddressService addressService;

    @GetMapping
    public Result<List<AddressVO>> addressList(){
        // 获取当前登录用户的id
        Integer userId = SecurityUserContext.currentUserId();
        // 根据用户id查询其下的收货地址
        List<AddressVO> addressVOS = addressService.listAddressByUserId(userId);
        return Result.success(addressVOS);
    }

    /**
     * 设置默认的收获地址
     */
    @PutMapping("/setDefault/{addressId}")
    public Result<?> setDefaultAddress(@PathVariable Integer addressId){
        addressService.modifyDefaultAddress(addressId,"set");
        return Result.success();
    }

    /**
     * 取消默认的收获地址
     */
    @PutMapping("/cancelDefault/{addressId}")
    public Result<?> cancelDefault(@PathVariable Integer addressId){
        addressService.modifyDefaultAddress(addressId,"cancel");
        return Result.success();
    }
}
